import request from './request';

// 订单接口类型定义
export interface OrderItem {
  productId: string;
  productName: string;
  price: number;
  quantity: number;
  subtotal: number;
}

export interface Order {
  _id: string;
  orderNumber: string;
  customer: {
    name: string;
    phone: string;
    address: string;
  };
  items: OrderItem[];
  totalAmount: number;
  deliveryFee: number;
  discountAmount: number;
  actualAmount: number;
  status: '待支付' | '待发货' | '配送中' | '已完成' | '已取消';
  paymentStatus: '未支付' | '已支付' | '已退款';
  deliveryStatus: '待发货' | '配送中' | '已送达' | '配送失败';
  paymentMethod: '微信支付' | '支付宝' | '现金';
  remark?: string;
  orderTime: string;
  paymentTime?: string;
  shipmentTime?: string;
  completionTime?: string;
}

export interface OrderListParams {
  page?: number;
  limit?: number;
  status?: string;
  paymentStatus?: string;
  deliveryStatus?: string;
  keyword?: string;
  startDate?: string;
  endDate?: string;
}

export interface CreateOrderData {
  customer: {
    name: string;
    phone: string;
    address: string;
  };
  items: {
    productId: string;
    quantity: number;
  }[];
  deliveryFee?: number;
  discountAmount?: number;
  paymentMethod?: string;
  remark?: string;
}

// 获取订单列表
export const getOrderList = (params: OrderListParams) => {
  return request.get('/api/order/list', { params });
};

// 获取订单详情
export const getOrderDetail = (id: string) => {
  return request.get(`/api/order/detail/${id}`);
};

// 创建订单
export const createOrder = (data: CreateOrderData) => {
  return request.post('/api/order/create', data);
};

// 更新订单状态
export const updateOrderStatus = (
  id: string, 
  data: {
    status?: string;
    paymentStatus?: string;
    deliveryStatus?: string;
  }
) => {
  return request.patch(`/api/order/status/${id}`, data);
};

// 删除订单
export const deleteOrder = (id: string) => {
  return request.delete(`/api/order/${id}`);
};

// 批量删除订单
export const batchDeleteOrders = (ids: string[]) => {
  return request.delete('/api/order/batch/delete', { data: { ids } });
};

// 获取订单统计
export const getOrderStats = () => {
  return request.get('/api/order/stats');
}; 